package sort;

import java.util.Random;

public class quickSort {
    public static void main(String[] args) {
        Random r = new Random();
        int []arr = new int[100000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt();
        }
        long start = System.currentTimeMillis();
        quick(arr,0,arr.length - 1);
        long end = System.currentTimeMillis();
        System.out.println(end-start);//57
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print(arr[i] + " ");
//        }
    }

    private static void quick(int[] arr, int i, int j) {
        int start = i;
        int end = j;
        if(start > end){
            return;
        }
        int baseNumber = arr[i];
        while (start != end){
            while (true){
                if(end <= start || arr[end] < baseNumber){
                    break;
                }
                end--;
            }
            while (true){
                if(end <= start || arr[start] > baseNumber){
                    break;
                }
                start++;
            }
            int temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
        }
        int temp = arr[i];
        arr[i] = arr[start];
        arr[start] = temp;
        quick(arr,i,start - 1);
        quick(arr,start + 1,j);
    }
}
